<?php
require_once APP_PATH.'models'.DS.'karyawan'.DS.'KaryawanBL.php';

class AbsensiAjaxController extends AppController {
   
   public $name = 'AbsensiAjax';
   public $uses = array();
   public $components = array('Session','RequestHandler');
   
   public function jadwalKerjaCreateSession() {
      $this->autoRender = false;
      
      $dataJadwal = array();

      $limit = (int)$this->params['url']['info']['jadwal_kerja_cycle'];
      $days = $this->params['url']['days'];

      if (!$this->Session->check('JadwalKerja')) {

         // --- set all values to zero
         for ($i=1; $i<=$limit; $i++) {
            $this->Session->write('JadwalKerja.jadwal_hari.'.$i.'.hari',0);
            $this->Session->write('JadwalKerja.jadwal_hari.'.$i.'.jam_kerja_id',0);
            $this->Session->write('JadwalKerja.jadwal_hari.'.$i.'.jadwal_name','empty');
         }
      }

      /*
       * --- if session JadwalKerja has been set
       * --- modify the value based on value that come from input
       * $days structure that come from ajax request :
       * 
       * [days] => Array (
       *    [0] => Array (
       *          [hari] => 1
       *    )
       * 
       *    [1] => Array (
       *          [hari] => 1
       *    )
       * 
       *    [x] => Array (
       *          [hari] => x
       *    )
       * )
       */

      $ct = 1;
      foreach ($days as $v) {
         if ($v['hari'] != 0) {
            $this->Session->write('JadwalKerja.jadwal_hari.'.$ct.'.hari',$v['hari']);
            $this->Session->write('JadwalKerja.jadwal_hari.'.$ct.'.jam_kerja_id',$v['jam_kerja_id']);
            $this->Session->write('JadwalKerja.jadwal_hari.'.$ct.'.jadwal_name',$v['jadwal_name']);
         }

         $ct++;
      }

      $sessJadwaKerja = $this->Session->read('JadwalKerja');
      
      $this->set('data', $sessJadwaKerja);
      $this->render('_ajax_jadwal_kerja_create_session');
      
   }
   
   public function jadwalKerjaDeleteSession() {
      $this->autoRender =false;
      
      if ($this->Session->read('JadwalKerja')) {
         $this->Session->delete('JadwalKerja');
      }
      
      $this->set('confirm_ok',array('Ok'));
      $this->render('_ajax_jadwal_kerja_delete_session');
   }
   
   public function getKaryawanByDivisi() {
      $this->autoRender = false;
      
      $karyawanBL = new KaryawanBL();
      
      $options = array(
          'conditions' => array('Karyawan.divisi_id' => $this->params['url']['divisi_id'])
      );
      
      $this->set('karyawan_list', $karyawanBL->karyawanFind('all', $options));
      $this->render('_ajax_get_karyawan_by_divisi');
   }
}